package com.hobart.learning.lagou.edu.blogSystem.controller;

import com.hobart.learning.lagou.edu.blogSystem.pojo.Article;
import com.hobart.learning.lagou.edu.blogSystem.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

/**
 * @author: Hobart Mr.Bai
 * @create: 2021-05-09 23:09
 * @description:
 * @Version 1.0
 **/
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/article")
    public String  getArticleForPage(Model model, Integer pageNum, Integer pageSize, Article article) {
        if(pageNum==null){
            pageNum = Integer.valueOf(0);
        }
        if(pageSize==null){
            pageSize = Integer.valueOf(2);
        }
        Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.unsorted());
        Page<Article> articles = articleService.getArticleAllForPage(pageable, article);
        model.addAttribute("articles", articles);
        return "client/index";
    }

    @GetMapping("/index")
    public String  index(Model model) {
        model.addAttribute("page", new PageImpl(Arrays.asList("id")));
        return "client/index";
    }

}
